package com.changgou.business.listener;

import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @program: changgou_parent
 * @author: zhanzhewan
 * @create: 2020-03-26 10:13
 **/

@Component
@RabbitListener(queues = "ad_update_queue")
public class ADlistener {

    @RabbitHandler
    public void test(String message){
        System.out.println("rabbit接受到的数据为"+message);
        String url = "http://192.168.43.31/ad_update?position="+message;
        // 创建okhttp对象 这个东西是目前最快的发送请求的技术
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        // 发送请求
        Call call = okHttpClient.newCall(request);
        // 调用发送请求后的回调方法 获取发送成功或失败的信息
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                System.out.println("发送失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("发送成功");
            }
        });

    }

}
